package com.ws.admin.index.web;

import cn.hutool.core.util.StrUtil;
import com.ws.common.base.BaseUtils;
import com.ws.common.constant.BaseConstants;
import com.ws.common.result.entity.ResultEntity;
import com.ws.common.utils.SessionUtils;
import com.ws.system.role.service.ISystemRoleService;
import com.ws.system.user.entity.SystemUser;
import com.ws.system.user.service.ISystemUserService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.apache.shiro.web.util.SavedRequest;
import org.apache.shiro.web.util.WebUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping
@CrossOrigin
public class IndexWeb {

    @Autowired
    ISystemUserService userService;
    @Autowired
    ISystemRoleService roleService;

    @RequestMapping("/login")
    public Object login(SystemUser user, HttpServletRequest request){
        Subject subject= SecurityUtils.getSubject();
        Map<String,Object> map=new HashMap<String,Object>();
        UsernamePasswordToken token=new UsernamePasswordToken(user.getUserName(),user.getPassword());

        try {
            subject.login(token);
        }catch (UnknownAccountException e){
            return ResultEntity.error("用户名不存在！");
        }catch (IncorrectCredentialsException e){
            return ResultEntity.error("密码错误！");
        }
        user=userService.readByUserName(user.getUserName());
        user.setPassword("");
        user.setRoles(roleService.getRolesByUserId(user.getId()));
        SessionUtils.set("user",user);
        return ResultEntity.succes(user);
    }

    @RequestMapping("/toLogin")
    @ResponseBody
    public ResultEntity toLogin(){
        return ResultEntity.error("332","用户未登录！");
    }

    @RequestMapping("/testRole")
    @ResponseBody
    public ResultEntity testRole(){
        boolean b=SecurityUtils.getSubject().hasRole("root");
        return ResultEntity.succes("你的角色"+b);
    }

    @RequestMapping("/regist")
    public ResultEntity regist(SystemUser user){

        user.setIsDeleted(BaseConstants.NO);
        user.setCreateTime(new Date());
        user.setUpdateTime(new Date());

        String userName = user.getUserName();
        if(StrUtil.isEmpty( userName )){
            return ResultEntity.error("用户名不能为空!");
        }
        if(StrUtil.isEmpty(user.getRealName())){
            user.setRealName(userName);
        }
        if(StrUtil.isEmpty(user.getId())){
            SystemUser t=userService.readByUserName( userName );
            if(t!=null){
                return ResultEntity.error("用户名已存在!");
            }
        }
        return ResultEntity.succes(userService.saveOrUpdate(user));
    }
}
